package com.cyl.manager.ums.service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.cyl.h5.config.SecurityUtil;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.annotation.Excel;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.service.ISysDeptService;

import cn.hutool.json.JSONUtil;
import io.swagger.annotations.ApiModelProperty;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cyl.manager.ums.mapper.FeedbackMapper;
import com.cyl.manager.ums.domain.entity.Feedback;
import com.cyl.manager.ums.domain.query.FeedbackQuery;
import com.cyl.manager.ums.domain.vo.FeedbackAndZcVO;

/**
 * 意见反馈Service业务层处理
 *
 *
 * @author zcc
 */
@Service
public class FeedbackService {
    @Autowired
    private FeedbackMapper feedbackMapper;
    @Autowired
    private ISysDeptService deptService;
    
    @Autowired
    private MemberService memberService;
    
    @Autowired
    private SysDeptMapper deptMapper;
    
    
    /**
     * 查询意见反馈
     *
     * @param id 意见反馈主键
     * @return 意见反馈
     */
    public Feedback selectById(Long id) {
        return feedbackMapper.selectById(id);
    }

    /**
     * 查询意见反馈列表
     *
     * @param query 查询条件
     * @param page 分页条件
     * @return 意见反馈
     */
    public List<Feedback> selectList(FeedbackQuery query, Pageable page) {
        if (page != null) {
            PageHelper.startPage(page.getPageNumber() + 1, page.getPageSize());
        }
        QueryWrapper<Feedback> qw = new QueryWrapper<>();
        
        Long deptZbjId = query.getDeptZbjId();
        if(deptZbjId!=null) {
            qw.eq("dept_zbj_id", deptZbjId);
        }
        
        Long deptTzId = query.getDeptTzId();
        if (deptTzId!=null) {
            qw.eq("dept_tz_id", deptTzId);
        }
        Long spreaduid = query.getSpreaduid();
        if (spreaduid!=null) {
            qw.eq("spreaduid", spreaduid);
        }
     
        Long deptDpId = query.getDeptDpId();
        if (deptDpId!=null) {
            qw.eq("dept_dp_id", deptDpId);
        }
        
        Long memberId = query.getMemberId();
        if (memberId!=null) {
            qw.eq("member_id", memberId);
        }
        
        Integer pjdj = query.getPjdj();
        if (pjdj!=null) {
            qw.eq("pjdj", pjdj);
        }
        
        Integer pjdx = query.getPjdj();
        if (pjdx!=null) {
            qw.eq("pjdx", pjdx);
        }
        
        String type = query.getType();
        if (!StringUtils.isEmpty(type)) {
            qw.eq("type", type);
        }

        String nickname = query.getNickname();
        if (!StringUtils.isEmpty(nickname)) {
            qw.like("nickname", nickname);
        }
        
        String content = query.getContent();
        if (!StringUtils.isEmpty(content)) {
            qw.like("content", content);
        }
        String images = query.getImages();
        if (!StringUtils.isEmpty(images)) {
            qw.eq("images", images);
        }
        String phone = query.getPhone();
        if (!StringUtils.isEmpty(phone)) {
            qw.like("phone", phone);
        }
        Integer handleStatus = query.getHandleStatus();
        if (handleStatus != null) {
            qw.eq("handle_status", handleStatus);
        }
        LocalDateTime handleTime = query.getHandleTime();
        if (handleTime != null) {
            qw.eq("handle_time", handleTime);
        }
        if (!StringUtils.isEmpty(query.getBeginTime()) && !StringUtils.isEmpty(query.getEndTime())){
            qw.ge("create_time", query.getBeginTime());
            qw.lt("create_time", query.getEndTime());
        }
        qw.orderByDesc("id");
        return feedbackMapper.selectList(qw);
    }

    /**
     * 	新增意见反馈
     *
     * @param feedback 意见反馈
     * @return 结果
     */
    public int insertPj(Feedback feedback) {
        feedback.setCreateTime(LocalDateTime.now());
        feedback.setHandleStatus(0);
        feedback.setType("2");//评价
        return feedbackMapper.insert(feedback);
    }
    
    /**
     * 	新增意见反馈
     *
     * @param feedback 意见反馈
     * @return 结果
     */
    public int insert(Feedback feedback) {
        feedback.setCreateTime(LocalDateTime.now());
        feedback.setHandleStatus(0);
        feedback.setType("1");//店长申请
        if(selectCount(feedback,0) > 0) {
	        throw new RuntimeException("审核中，请勿重复提交");
        }
        if(selectCount(feedback,1) > 0) {
	        throw new RuntimeException("审核已通过，请勿重复提交");
        }
        return feedbackMapper.insert(feedback);
    }
    
    /**
     * 	查询未审核的个数
     * @param feedback
     * @return
     */
    public int selectCount(Feedback feedback,int handle_status) {
        QueryWrapper<Feedback> queryWrapper = new QueryWrapper<Feedback>();
        queryWrapper.eq("type", "1");
        queryWrapper.eq("handle_status", handle_status);
        queryWrapper.eq("member_id", feedback.getMemberId());
        return feedbackMapper.selectCount(queryWrapper);
    }
    
    
    /**
     * 修改意见反馈
     *
     * @param feedback 意见反馈
     * @return 结果
     */
    public int updatePj(Feedback feedback) {
    	UpdateWrapper<Feedback> updateWrapper = new UpdateWrapper<Feedback>();
    	Integer handle_status = feedback.getHandleStatus();
    	if(handle_status!=null && handle_status.intValue() == 2) {
        	updateWrapper.set("handle_status", feedback.getHandleStatus());
    	}
    	updateWrapper.set("content", feedback.getContent());
    	updateWrapper.set("images", feedback.getImages());
    	updateWrapper.set("pjdj", feedback.getPjdj());
    	updateWrapper.set("pjdx",feedback.getPjdx());
    	updateWrapper.eq("type", "2");
    	updateWrapper.eq("id", feedback.getId());
    	return feedbackMapper.update(null, updateWrapper);
    	
    }
    
    

    /**
     * 修改意见反馈
     *
     * @param feedback 意见反馈
     * @return 结果
     */
    public int update(Feedback feedback) {
        return feedbackMapper.updateById(feedback);
    }

    /**
     * 删除意见反馈信息
     *
     * @param id 意见反馈主键
     * @return 结果
     */
    public int deleteById(Long id) {
        return feedbackMapper.deleteById(id);
    }

    public Integer updateMark(Feedback feedback) {
        UpdateWrapper<Feedback> updateWrapper = new UpdateWrapper<>();
        updateWrapper
        .set("create_by",SecurityUtils.getUserId())
        .set("remark",feedback.getRemark())
                .eq("id",feedback.getId());
        return feedbackMapper.update(null,updateWrapper);
    }

    public Integer changeStatus(Feedback dto) {
        UpdateWrapper<Feedback> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", dto.getId());
        wrapper.set("create_by",SecurityUtils.getUserId())
		        .set("handle_status", dto.getHandleStatus())
                .set("handle_time",LocalDateTime.now());
        return feedbackMapper.update(null, wrapper);
    }
    
    
    @Transactional
    public SysDept updateFeedbackAndZc(FeedbackAndZcVO feedback) {
    	
    	SysDept sysDept = feedback.getSysDept();
    	if (UserConstants.NOT_UNIQUE.equals(deptService.checkDeptNameUnique(sysDept))){
	           throw new RuntimeException("店铺名称'" + sysDept.getDeptName() + "'失败，店铺名称已存在，请修改店铺名称");
        }
    	sysDept.setCreateBy(SecurityUtils.getUserId());
    	sysDept.setBmlx("5");
    	 deptService.insertDept(sysDept);
    	 
        SysDept retSysDept = deptMapper.checkDeptNameUnique(sysDept.getDeptName(), sysDept.getParentId());

    	memberService.updateByDz(retSysDept.getDeptId(), feedback.getMemberId(), 2);
    	
        UpdateWrapper<Feedback> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("create_by",SecurityUtils.getUserId())
	        .set("remark",feedback.getRemark())
	        .set("handle_status", feedback.getHandleStatus())
	        .set("handle_time",LocalDateTime.now())
                .eq("id",feedback.getId());
         feedbackMapper.update(null,updateWrapper);
        return retSysDept;
    }
}
